package com.stu.stusystem.controller;

import com.stu.stusystem.common.ApiException;
import com.stu.stusystem.common.ApiResult;
import com.stu.stusystem.service.LoginService;
import com.stu.stusystem.service.dto.RegisterDTO;
import com.stu.stusystem.service.dto.StuUserDTO;
import com.stu.stusystem.service.vo.LoginVO;
import com.stu.stusystem.util.ValidateCodeUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author cxt
 * @date 2020/9/9
 */
@Controller
@Slf4j
@Api(tags = "登录接口")
public class LoginController {

    private LoginService loginService;

    @GetMapping(value = {"/index", ""})
    @ApiOperation("首页")
    public String index() {
        return "/index";
    }

    @GetMapping("/notLogin")
    @ResponseBody
    @ApiOperation("未登录")
    public ApiResult<String> notLogin() {
        return ApiResult.error(401, "未登录或会话超时，请重新登录!");
    }

    @GetMapping("/logout")
    @ResponseBody
    @ApiOperation("登出")
    public ApiResult<String> logout(HttpServletRequest request) {
        loginService.logout(request);
        return ApiResult.success("登出系统成功！");
    }

    @PostMapping(value = "/login")
    @ResponseBody
    @ApiOperation("登录验证")
    public ApiResult<LoginVO> login(@RequestBody StuUserDTO stu, HttpServletRequest request) {
//        loginService.verification(stu.getVerification(), request);
        LoginVO verification = loginService.verification(stu, request);
        return ApiResult.success("登录成功！", verification);
    }

    @PostMapping("/register")
    @ResponseBody
    @ApiOperation("注册用户")
    public ApiResult<String> registers(@RequestBody RegisterDTO registerDTO) {
        loginService.register(registerDTO);
        return ApiResult.success("注册成功！");
    }

    @ApiOperation("更新账号密码")
    @ResponseBody
    @GetMapping("/updatePwd")
    public ApiResult<String> updateUserPwd(@RequestParam("id") String id, @RequestParam("oldPwd") String oldPwd, @RequestParam("newPwd") String newPwd) {
        String account = loginService.updateUserPwd(id, oldPwd, newPwd);
        return ApiResult.success("用户{"+account+"}密码更新成功!");
    }

    @GetMapping(value = "/getVerify")
    @ApiOperation("获取验证码")
    public void getVerify(HttpServletRequest request, HttpServletResponse response) {
        try {
            response.setContentType("image/jpeg");                            // 设置相应类型,告诉浏览器输出的内容为图片
            response.setHeader("Pragma", "No-cache");                   // 设置响应头信息，告诉浏览器不要缓存此内容
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expire", 0);
            ValidateCodeUtil randomValidateCode = new ValidateCodeUtil();
            randomValidateCode.getRandcode(request, response);                // 输出验证码图片方法
        } catch (Exception e) {
            log.debug("获取验证码失败：" + e);
            throw new ApiException("获取验证码失败,请刷新页面");
        }
    }

    @Autowired
    public void setLoginService(LoginService loginService) {
        this.loginService = loginService;
    }
}
